home *** CD-ROM | disk | FTP | other *** search
/ AppleScript - The Beta Release / AppleScript - The Beta Release.iso / Documentation / develop / Better Apple Event Coding / Code Samples / Document.cp < prev    next >
Encoding:
Text File  |  1992-10-16  |  5.1 KB  |  268 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------------------
  2.  
  3.     Program:    CPlusTESample 2.0
  4.     File:        Document.cp
  5.     Uses:       Document.h
  6.  
  7.     by Andrew Shebanow
  8.     of Apple Macintosh Developer Technical Support
  9.  
  10.     Copyright © 1989-1990 Apple Computer, Inc.
  11.     All rights reserved.
  12.  
  13. ------------------------------------------------------------------------------------------*/
  14.  
  15. // Mac Includes
  16. #ifndef __TYPES__
  17. #include <Types.h>
  18. #endif
  19. #ifndef __QUICKDRAW__
  20. #include <QuickDraw.h>
  21. #endif
  22. #ifndef __FONTS__
  23. #include <Fonts.h>
  24. #endif
  25. #ifndef __EVENTS__
  26. #include <Events.h>
  27. #endif
  28. #ifndef __CONTROLS__
  29. #include <Controls.h>
  30. #endif
  31. #ifndef __WINDOWS__
  32. #include <Windows.h>
  33. #endif
  34. #ifndef __MENUS__
  35. #include <Menus.h>
  36. #endif
  37. #ifndef __TEXTEDIT__
  38. #include <TextEdit.h>
  39. #endif
  40. #ifndef __DIALOGS__
  41. #include <Dialogs.h>
  42. #endif
  43. #ifndef __DESK__
  44. #include <Desk.h>
  45. #endif
  46. #ifndef __SCRAP__
  47. #include <Scrap.h>
  48. #endif
  49. #ifndef __TOOLUTILS__
  50. #include <ToolUtils.h>
  51. #endif
  52. #ifndef __MEMORY__
  53. #include <Memory.h>
  54. #endif
  55. #ifndef __SEGLOAD__
  56. #include <SegLoad.h>
  57. #endif
  58. #ifndef __FILES__
  59. #include <Files.h>
  60. #endif
  61. #ifndef __OSUTILS__
  62. #include <OSUtils.h>
  63. #endif
  64. #ifndef __TRAPS__
  65. #include <Traps.h>
  66. #endif
  67. #ifndef __PACKAGES__
  68. #include <Packages.h>
  69. #endif
  70. #ifndef __ERRORS__
  71. #include <Errors.h>
  72. #endif
  73.  
  74. #include "Document.h"
  75.  
  76. //---------------------------------------------------------------------------------
  77.  
  78. TDocument::TDocument(short resID, OSType fileType)
  79. {
  80.     short wdRefNum, vRefNum;
  81.     long dirID;
  82.  
  83.     (void) GetVol(nil,&wdRefNum);
  84.     TApplication::WDToDirID(wdRefNum,vRefNum,dirID);
  85.     fFile.vRefNum = vRefNum;
  86.     fFile.parID = dirID;
  87.     CopyPString(fFile.name,"\pUntitled");
  88.  
  89.     fFileRefNum = 0;
  90.     fNewDoc = true;
  91.     fReadOnly = false;
  92.     fDirty = false;
  93.     fFileType = fileType;
  94.     fActive = false;
  95.  
  96.     fDocWindow = GetNewWindow(resID,nil,(WindowPtr) -1);
  97.     SetPort(fDocWindow);
  98. }
  99.  
  100. TDocument::~TDocument()
  101. {
  102.     if (fFileRefNum != 0)
  103.       CloseFile();
  104.     DisposeWindow(fDocWindow);
  105. }
  106.  
  107. void TDocument::DoActivate(Boolean becomingActive)
  108. {
  109.     fActive = becomingActive;
  110. }
  111.  
  112. void TDocument::OpenFile(Boolean readOnly, Boolean createIfNecessary)
  113. {
  114.     short refNum;
  115.     char perm;
  116.     FInfo fndrInfo;
  117.  
  118.     // first, check if file exists
  119.     OSErr err = HGetFInfo(fFile.vRefNum,fFile.parID,fFile.name,&fndrInfo);
  120.     if ((err == fnfErr) && createIfNecessary)
  121.       {
  122.         FailOSErr(HCreate(fFile.vRefNum,fFile.parID,fFile.name,
  123.                       TApplication::GetCreator(),fFileType));
  124.       }
  125.     else FailOSErr(err);
  126.  
  127.     perm = (readOnly) ? fsRdPerm : fsRdWrPerm;
  128.     FailOSErr(HOpen(fFile.vRefNum, fFile.parID,
  129.                     fFile.name, perm, &refNum));
  130.     fFileRefNum = refNum;
  131. }
  132.  
  133. void TDocument::CloseFile()
  134. {
  135.     if (fFileRefNum != 0)
  136.       {
  137.         FailOSErr(FSClose(fFileRefNum));
  138.         fFileRefNum = 0;
  139.       }
  140. }
  141.  
  142. void TDocument::OpenOldDoc(FSSpec theFile, Boolean readOnly)
  143. {
  144.     fFile = theFile;
  145.     fFileRefNum = 0;
  146.     fNewDoc = false;
  147.     fReadOnly = readOnly;
  148.     fDirty = false;
  149.  
  150.     OpenFile(fReadOnly,false);
  151.     (void) SetFPos(fFileRefNum,fsFromStart,0);
  152.     ReadFromFile(fFileRefNum);
  153.  
  154.     SetWTitle(fDocWindow,fFile.name);
  155.     SetPort(fDocWindow);
  156. }
  157.  
  158. YNCResult TDocument::PresentSaveDialog(Boolean quitting)
  159. {
  160.     Str255 fileName;
  161.     Str255 buzzword;
  162.  
  163.     SetCursor(&qd.arrow);
  164.     CopyPString(fileName,fFile.name);
  165.     if (quitting)
  166.       GetIndString(buzzword,kBuzzwordStrings,bQuitting);
  167.     else GetIndString(buzzword,kBuzzwordStrings,bClosing);
  168.     ParamText(fileName, buzzword, "\p", "\p");
  169.     short item = Alert(rSaveAlert, (ModalFilterProcPtr) nil);
  170.     switch (item)
  171.       {
  172.         case 1:
  173.         default:
  174.             return yesResult;
  175.         case 2:
  176.             return noResult;
  177.         case 3:
  178.             return cancelResult;
  179.       }
  180. }
  181.  
  182. YNCResult TDocument::DoClose(Boolean askUserToSave, YNCResult defaultAnswer, Boolean quitting)
  183. {
  184.     YNCResult res;
  185.  
  186.     if (fDirty && askUserToSave)
  187.       res = PresentSaveDialog(quitting);
  188.     else res = defaultAnswer;
  189.     if (fDirty && (res == yesResult))
  190.       DoSave();
  191.     return res;
  192. }
  193.  
  194. void TDocument::DoSave()
  195. {
  196.     if (fNewDoc)
  197.       {
  198.         DoSaveAs();
  199.         return;
  200.       }
  201.  
  202.     if (!fDirty)
  203.       return;
  204.  
  205.     FailOSErr(SetFPos(fFileRefNum,fsFromStart,0));
  206.     FailOSErr(SetEOF(fFileRefNum,0));
  207.     WriteToFile(fFileRefNum);
  208.     fDirty = false;
  209. }
  210.  
  211. void TDocument::DoSaveAs()
  212. {
  213.     Point where;
  214.     Str255 prompt, origName;
  215.     SFReply reply;
  216.  
  217.     SetPt(&where,100,100);
  218.     CopyPString(origName,fFile.name);
  219.     prompt[0] = '\0';
  220.     SFPutFile(where, prompt, origName, (DlgHookProcPtr) nil, &reply);
  221.     if (reply.good == false)
  222.       return;
  223.  
  224.     // close existing file, if any
  225.     if (fFileRefNum != 0)
  226.       CloseFile();
  227.  
  228.     // setup fields correctly
  229.     TApplication::WDToDirID(reply.vRefNum, fFile.vRefNum, fFile.parID);
  230.     CopyPString(fFile.name, reply.fName);
  231.  
  232.     OpenFile(false,true);
  233.     SetWTitle(fDocWindow,fFile.name);
  234.  
  235.     FailOSErr(SetFPos(fFileRefNum,fsFromStart,0));
  236.     WriteToFile(fFileRefNum);
  237.  
  238.     fNewDoc = false;
  239.     fDirty = false;
  240. }
  241.  
  242. //---------------------------------------------------------------------------------
  243.  
  244. // find the TDocument associated with the window
  245. TDocument* TDocumentList::FindDoc(WindowPtr window)
  246. {
  247.     TDocument* tDoc;
  248.  
  249.     for (int idx = 0; idx < fNumObjs; idx++)
  250.       {
  251.         tDoc = (TDocument*) At(idx);
  252.         if (tDoc->GetDocWindow() == window)
  253.           return tDoc;
  254.       }
  255.     return nil;
  256. }
  257.  
  258. // private list management routines
  259. void TDocumentList::AddDoc(TDocument* doc)
  260. {
  261.     InsertFirst(doc);
  262. }
  263.  
  264. void TDocumentList::RemoveDoc(TDocument* doc)
  265. {
  266.     Remove(doc);
  267. }
  268.